<template>
  <!--直播列表新增/编辑弹窗-->
  <a-modal  :maskClosable="false"
            :title="mdl.dialogTitleMap[mdl.dialogStatus]"
            :width="1000"
            @ok="mdl.dialogStatus === 'create' ? createData() : updateData()"
            v-model="mdl.visible"
            :confirm-loading="loading"
  >
    <a-form>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="直播名称"
      >
        <a-input placeholder="" v-model="mdl.formData.liveName"/>
      </a-form-item>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="是否公开"
      >
        <a-radio-group v-model="mdl.formData.showType" @change="changeOpenIf">
          <a-radio :value="0">
            公开
          </a-radio>
          <a-radio :value="1">
            不公开
          </a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="指定观看人群"
        v-if="openIfShow"
      >
        <a-tree-select
          v-model="specifyPersonList"
          style="width: 100%"
          :tree-data="sysDept"
          tree-checkable
          placeholder="请选择指定观看人群"
          search-placeholder="Please select"
        />
      </a-form-item>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="讲师名称"
      >
        <a-input placeholder="" v-model="mdl.formData.liveTeacherName"/>
      </a-form-item>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="讲师介绍"
      >
        <a-textarea :rows="4" v-model="mdl.formData.liveTeacherInfo"/>
      </a-form-item>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="积分"
      >
        <a-input-number v-model="mdl.formData.integral" :min="1"/>
      </a-form-item>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="学时"
      >
        <a-input-number v-model="mdl.formData.period" :min="1"/>
      </a-form-item>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="直播介绍"
      >
        <a-textarea :rows="4" v-model="mdl.formData.liveInfo"/>
      </a-form-item>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="直播时间"
      >
        <a-date-picker @change="onChange" :value="moment(dataDay, dateFormat)"/>
        <a-select v-model="dataTime" allowClear style="width: 120px;">
          <a-select-option :value="item" v-for="item in datarq">{{item}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="封面图"
      >
        <coverMap :id.sync="mdl.formData.picFileNo" @getImgFile="getImgFile" :msg.sync="mdl.formData.picFileNo"/>
      </a-form-item>
      <a-form-item label="直播视频" :labelCol="mdl.labelCol"
                   :wrapperCol="mdl.wrapperCol">
        <videoCom @getVideoFile="getVideoFile" :msg.sync="mdl.formData.liveFileName" :id.sync="mdl.formData.liveFileNo"></videoCom>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
  import moment from 'moment';
  import coverMap from '../../homePages/coverMap'
  import {liveInsert,liveup,querySpecifyPerson,querySysDeptByChildren,querySysDeptByChildrenLong,querysysDept} from '@/api/courseManage/live'
  import videoCom from '@/components/videoCom'
  export default {
    components: {
      coverMap,
      videoCom
    },
    props: {
      /*   -------弹窗--------*/
      mdl: [Array,Object]
    },
    data() {
      return {
        datarq:['00','01','02','03','04','05','06','07','08','09','10',
          '11','12','13','14','15','16','17','18','19','20',
          '21','22','23'],
        loading: false,
        dataTime: '',
        dataDay: '',
        dateFormat: 'YYYY-MM-DD',
        specifyPersonList: [],//指定观看人群指定的
        openIfShow: false,//指定观看人群是否能选择
        sysDept: [],
        value: [],
      }
    },
    inject: ['upShow'],
    watch: {
//        直播时间
      liveTime(value) {
        this.dataDay = value.split(' ')[0]
        this.dataTime = value.split(' ')[1].split(':')[0]
      },
//      获取指定观看人群
      id(value) {
          if(this.mdl.dialogStatus!= 'create') {
//            this.specifyPersonList = []
            this.getSpecifyPerson(value)
          }
      },
      showType(value) {
        this.openIfShow = value == 0 ? false : true
      },
    },
    created() {
      this.getQuerysysDept()
    },
    computed: {
      liveTime() {
          return this.mdl.formData.liveTime
      },
      id() {
          return this.mdl.formData.id
      },
      showType() {
        return this.mdl.formData.showType
      }
    },
    methods: {
      moment,
      // 获取部门树状结构
      getQuerysysDept() {
        this.sysDept = []
        querysysDept().then(res=> {
          if (res.code === 200) {
            res.data.forEach(item=>{
              var childrens = []
              item.children.forEach(i=> {
                childrens.push({
                  title:i.name,
                  value: i.id,
                  key: i.id,
                  children: []
                })
              })
              this.sysDept.push({
                title:item.name,
                value: item.id,
                key: item.id,
                children: childrens
              })
            })

            querySpecifyPerson(-1).then(res=> {
              if(res.code === 200) {
                res.data.forEach(item=> {
                  if(item.deptParentId == 0) {
                    let index = this.sysDept.findIndex( i => i.key == item.deptId)
                    this.sysDept[index].children.push({
                      title:item.userName,
                      value: item.id,
                      key: item.id,
                    })
                  }else {
                    let index1 = this.sysDept.findIndex( i => i.key == item.deptParentId)
                    let arr = this.sysDept[index1].children
                    arr[arr.findIndex( i => i.key == item.deptId)].children.push({
                      title:item.userName,
                      value: item.id,
                      key: item.id,
                    })
                    this.sysDept[index1].children = arr
                  }
                })
                console.log('this.sysDept：' + JSON.stringify(this.sysDept));
              } else {

              }
            }).catch(err => {

            })
          }
        })
      },
//      获取指定观看人群
      getSpecifyPerson(id) {
        querySpecifyPerson(id).then(res=> {
          if(res.code === 200) {
            this.specifyPersonList = []
            if(res.data.length!=0) {
              res.data.forEach(item=> {
                  if(item.check==1) {
                    this.specifyPersonList.push(item.id)
                  }
              })
            }
          } else {

          }
        }).catch(err => {

        })
      },
      handleChange(value) {
        console.log(`Selected: ${value}`);
      },
      popupScroll() {
        console.log('popupScroll');
      },
//          新增
      createData() {
        this.loading = true
        this.mdl.formData.createTime = moment().format('YYYY-MM-DD HH:mm:ss')
        this.mdl.formData.liveTime = this.dataDay + ' ' + this.dataTime
        this.specifyPersonList.forEach(item=> {
          this.mdl.formData.userRelList.push({
            "liveId": '',
            "userId": item
          })
        })
        const parameter = this.mdl.formData
        liveInsert(parameter).then(res => {
          if(res.code === 200) {
            this.loading = false
            this.upShow(1,"新增成功")
            this.mdl.visible = false
            this.$emit('handleSearch')
          } else {
            this.loading = false
            this.upShow(2,res.msg)
          }
        }).catch(err => {
          this.loading = false
          this.upShow(2)
        })
      },
//      编辑
      updateData() {
        this.loading = true
        this.mdl.formData.liveTime = this.dataDay + ' ' + this.dataTime
        const parameter = this.mdl.formData
        liveup(parameter).then(res => {
          if(res.code === 200) {
            this.loading = false
            this.upShow(1,"编辑成功")
            this.mdl.visible = false
            this.$emit('handleSearch')
          } else {
            this.loading = false
            this.upShow(2,res.msg)
          }
        }).catch(err => {
          this.loading = false
          this.upShow(2)
        })
      },
//      选择日期
      onChange(date, dateString) {
        console.log(date, dateString);
        this.dataDay = dateString
      },
      // 获取上传的视频数据
      getVideoFile(val) {
          if(val == '') {
            this.mdl.formData.liveFileName= val;
            this.mdl.formData.liveFileNo = val;
            this.mdl.formData.liveFilePath = val;
            this.mdl.formData.livePeriod = val;
          }else {
            this.mdl.formData.liveFileName= val.fileName;
            this.mdl.formData.liveFileNo = val.fileNo;
            this.mdl.formData.liveFilePath = val.fileUrl;
            this.mdl.formData.livePeriod = val.filePeriod;
            if(this.mdl.formData.liveName == '') {
              this.mdl.formData.liveName = val.fileName;
            }
          }
      },
      // 获取上传的图片数据
      getImgFile() {
        if(val == '') {
          this.mdl.formData.picFileNo = val;
        }else {
          this.mdl.formData.picFileNo = val.fileNo;
        }
      },
      changeOpenIf(e) {
//        console.log('radio checked:', e.target.value);
        this.mdl.formData.showType = e.target.value
      }
    }
  }
</script>